#include "imagelistview.h"
#include "mainwindow.h"

#include <QtWidgets>

ImageListView::ImageListView(QWidget *parent)
    : QListWidget{parent}
{
    setViewMode(QListView::IconMode);
    setIconSize(QSize(100, 100));
    setSpacing(10);
    setResizeMode(QListView::Adjust);
}

void ImageListView::setDir(const QString &dir)
{
    this->clear();
    auto list = QDir(dir).entryInfoList(QDir::Files);
    for(int k = 0; k < list.count(); k ++) {
        auto info = list[k];
        QListWidgetItem *item = new QListWidgetItem;
        item->setIcon(QIcon(QPixmap(info.filePath()).scaledToWidth(100)));
        item->setData(Qt::UserRole, info.filePath());
        item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
        this->addItem(item);

        if(MainWindow *w = qobject_cast<MainWindow *>(this->window())) {
            w->updateListInfo(QString("%3:%1/%2").arg(k + 1).arg(list.count()).arg(dir));
        }
        QApplication::processEvents(QEventLoop::WaitForMoreEvents);
    }
}
